const { prot } = require("../config/conifg")
const Koa = require('koa');
const websockify = require('koa-websocket');
const koaBody = require('koa-body');
const staticFiles = require('koa-static');
const router = require("./router");
const wsRouter = require("./router/wsRouter");
const { koaJwt } = require("./utils/jwt");
const app = websockify(new Koa());

app.use(staticFiles('./dist'))
app.use(async (ctx, next) => {
    ctx.set('Access-Control-Allow-Origin', '*')
    ctx.set('Access-Control-Allow-Headers', 'Content-Type,Content-Length,authorization,Accept,X-Requested-With')
    ctx.set('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
    if (ctx.method == 'OPTIONS') {
        ctx.body = 200;
    } else {
        await next()
    }
})
app.use(koaBody({
    multipart: true,
    formidable: {
        maxFileSize: 200 * 1024 * 1024
    }
}));
app.use(koaJwt);
/**
 * 启动路由
 */
router(app);
/**
 * 启动ws路由
 */
wsRouter(app);
app.listen(prot, () => {
    console.log('http://localhost:' + prot);
    console.log('已经启动');
});